package com.danger.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.danger.Activity.Fragment.BackHandledFragment;
import com.danger.Activity.Fragment.BackHandledInterface;
import com.danger.Activity.Fragment.MainFragment;

public class MainActivity extends AppCompatActivity implements BackHandledInterface {

    private BackHandledFragment selectedFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new MainFragment()).commit();
        }


    }

    @Override
    public void onBackPressed() {
        if (selectedFragment == null || !selectedFragment.onBackPressed()) {
            if (getFragmentManager().getBackStackEntryCount() == 0) {
                super.onBackPressed();
            } else {
                getFragmentManager().popBackStack();
            }
        }
    }

    @Override
    public void setSelectedFragment(BackHandledFragment selectedFragment) {
        this.selectedFragment = selectedFragment;
    }
}
